<template>
  <el-dialog
      :title="dataForm.id ? '编辑项目' : '新增项目'"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="50%">
    <el-form
        :model="dataForm"
        :rules="dataRule"
        ref="dataForm"
        label-width="120px"
        label-position="right"
        @keyup.enter.native="dataFormSubmit">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="项目编号" prop="projectCode">
            <el-select v-model="dataForm.projectCode" placeholder="请选择项目编号" class="label_len" :disabled="selectDis">
              <el-option v-for="item in projectList" :key="item.prProjectNumber"
                         :label="item.prProjectNumber + item.prProjectName" :value="item.prProjectNumber">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="测定日期" prop="detectionTime">
            <el-date-picker v-model="dataForm.detectionTime" type="datetime" placeholder="测定日期" format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="质量(kg)" prop="quality1">
            <el-input v-model="dataForm.quality1" placeholder="请输入＜4cm质量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="质量(kg)" prop="quality2">
            <el-input v-model="dataForm.quality2" placeholder="请输入4cm~8cm质量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="质量(kg)" prop="quality3">
            <el-input v-model="dataForm.quality3" placeholder="请输入＞8cm质量"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="占总量(%)" prop="accounted1">
            <el-input v-model="dataForm.accounted1" placeholder="＜4cm占总量" readonly disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="占总量(%)" prop="accounted2">
            <el-input v-model="dataForm.accounted2" placeholder="4cm~8cm占总量" readonly disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="占总量(%)" prop="accounted3">
            <el-input v-model="dataForm.accounted3" placeholder="＞8cm占总量" readonly disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="总量(kg)" prop="qualityCount">
            <el-input v-model="dataForm.qualityCount" placeholder="总量" readonly disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="margin-top: 20px">
        <el-col :span="12">
          <el-form-item label="碎土率计算值" prop="crushedSoil">
            <el-input v-model="dataForm.computeCrushedSoil" placeholder="碎土率" readonly disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="碎土率校验值">
            <el-input v-model="dataForm.crushedSoil" placeholder="" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          projectCode: '',
          detectionTime: '',
          quality1: '',
          quality2: '',
          quality3: '',
          accounted1: '',
          accounted2: '',
          accounted3: '',
          qualityCount: '',
          crushedSoil: '',
          inspector: '',
          notekeeper: '',
          checker: '',
          pdfPath: '',
          wordPath: '',
          status: '',
          userId: '',
          auditId: '',
          auditOpinion: '',
          auditDate: '',
          computeCrushedSoil:''
        },
        selectDis:false,
        projectList:[],
        dataRule: {
          projectCode: [
            { required: true, message: '项目编号不能为空', trigger: 'blur' }
          ],
          detectionTime: [
            { required: true, message: '检测日期不能为空', trigger: 'blur' }
          ],
          quality1: [
            { required: true, message: '质量1不能为空', trigger: 'blur' }
          ],
          quality2: [
            { required: true, message: '质量2不能为空', trigger: 'blur' }
          ],
          quality3: [
            { required: true, message: '质量3不能为空', trigger: 'blur' }
          ],
          accounted1: [
            { required: true, message: '占总量1不能为空', trigger: 'blur' }
          ],
          accounted2: [
            { required: true, message: '占总量2不能为空', trigger: 'blur' }
          ],
          accounted3: [
            { required: true, message: '占总量3不能为空', trigger: 'blur' }
          ],
          qualityCount: [
            { required: true, message: '总量不能为空', trigger: 'blur' }
          ],
          crushedSoil: [
            { required: true, message: '碎土率不能为空', trigger: 'blur' }
          ],
          inspector: [
            { required: true, message: '检测人不能为空', trigger: 'blur' }
          ],
          notekeeper: [
            { required: true, message: '记录人不能为空', trigger: 'blur' }
          ],
          checker: [
            { required: true, message: '核校人不能为空', trigger: 'blur' }
          ],
          pdfPath: [
            { required: true, message: 'pdf路径不能为空', trigger: 'blur' }
          ],
          wordPath: [
            { required: true, message: 'word路径不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '审核状态不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户ID不能为空', trigger: 'blur' }
          ],
          auditId: [
            { required: true, message: '审核人不能为空', trigger: 'blur' }
          ],
          auditOpinion: [
            { required: true, message: '审核意见不能为空', trigger: 'blur' }
          ],
          auditDate: [
            { required: true, message: '审核时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    watch: {
      'dataForm.quality1'() {
        this.updateTotal();
      },
      'dataForm.quality2'() {
        this.updateTotal();
      },
      'dataForm.quality3'() {
        this.updateTotal();
      },
      'dataForm.qualityCount'() {
        this.updateCrushedSoil();
      },
    },
    mounted() {
      this.getprojectList()
    },
    methods: {
      updateTotal() {
        const quality1 = parseFloat(this.dataForm.quality1) || 0;
        const quality2 = parseFloat(this.dataForm.quality2) || 0;
        const quality3 = parseFloat(this.dataForm.quality3) || 0;
        const total = quality1 + quality2 + quality3;

        this.dataForm.qualityCount = total;

        // 使用 Math.round 进行四舍五入并保留一位小数
        this.dataForm.accounted1 = total > 0 ? Math.round((quality1 / total) * 1000) / 10 : 0;
        this.dataForm.accounted2 = total > 0 ? Math.round((quality2 / total) * 1000) / 10 : 0;
        this.dataForm.accounted3 = total > 0 ? Math.round((quality3 / total) * 1000) / 10 : 0;
      },

      updateCrushedSoil() {
        const quality1 = parseFloat(this.dataForm.quality1) || 0;
        const total = parseFloat(this.dataForm.qualityCount) || 0;

        // 使用 Math.round 四舍五入并去掉小数部分
        const solt = total > 0 ? Math.round((quality1 / total) * 100) : 0;
        this.dataForm.crushedSoil = solt;
        this.dataForm.computeCrushedSoil = solt;
      },
      //获取项目列表
      getprojectList() {
        this.$http({
          url: this.$http.adornUrl(`/reportapproval/product/getProjectNumber`),
          method: 'post',
          data: this.$http.adornData({
          })
        }).then((data) => {
          if (data && data.code === 0) {
            console.log(data.productList)
            this.projectList = data.productList
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.selectDis = false
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/reportapproval/breakagerate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 0) {
                this.dataForm.projectCode = data.breakageRate.projectCode
                this.dataForm.detectionTime = data.breakageRate.detectionTime
                this.dataForm.quality1 = data.breakageRate.quality1
                this.dataForm.quality2 = data.breakageRate.quality2
                this.dataForm.quality3 = data.breakageRate.quality3
                this.dataForm.accounted1 = data.breakageRate.accounted1
                this.dataForm.accounted2 = data.breakageRate.accounted2
                this.dataForm.accounted3 = data.breakageRate.accounted3
                this.dataForm.qualityCount = data.breakageRate.qualityCount
                this.dataForm.crushedSoil = data.breakageRate.crushedSoil
                this.dataForm.inspector = data.breakageRate.inspector
                this.dataForm.notekeeper = data.breakageRate.notekeeper
                this.dataForm.checker = data.breakageRate.checker
                this.dataForm.pdfPath = data.breakageRate.pdfPath
                this.dataForm.wordPath = data.breakageRate.wordPath
                this.dataForm.status = data.breakageRate.status
                this.dataForm.userId = data.breakageRate.userId
                this.dataForm.auditId = data.breakageRate.auditId
                this.dataForm.auditOpinion = data.breakageRate.auditOpinion
                this.dataForm.auditDate = data.breakageRate.auditDate
                this.dataForm.computeCrushedSoil = ''
                this.selectDis = true
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/reportapproval/breakagerate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'projectCode': this.dataForm.projectCode,
                'detectionTime': this.dataForm.detectionTime,
                'quality1': this.dataForm.quality1,
                'quality2': this.dataForm.quality2,
                'quality3': this.dataForm.quality3,
                'accounted1': this.dataForm.accounted1,
                'accounted2': this.dataForm.accounted2,
                'accounted3': this.dataForm.accounted3,
                'qualityCount': this.dataForm.qualityCount,
                'crushedSoil': this.dataForm.crushedSoil,
                'inspector': this.dataForm.inspector,
                'notekeeper': this.dataForm.notekeeper,
                'checker': this.dataForm.checker,
                'pdfPath': this.dataForm.pdfPath,
                'wordPath': this.dataForm.wordPath,
                'status': this.dataForm.status,
                'userId': this.dataForm.userId,
                'auditId': this.dataForm.auditId,
                'auditOpinion': this.dataForm.auditOpinion,
                'auditDate': this.dataForm.auditDate
              })
            }).then((data) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.warning(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
